package queuedemo;
public class MyQueue {
    //双链表实现
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
        public ListNode head;
        public ListNode last;
        /**
         * 入队,尾插
         */
        public void offer(int val) {
            ListNode node = new ListNode(val);
            if(head == null) {
                head = node;
                last = node;
            }
            node.prev = last;
            last.next = node;
            last = node;
        }
        /**
         * 出队
         */
        public int poll() {
            if(head == null) {
                return -1;
            }
            if(head.next == null) {
                head = null;
                last = null;
                return head.val;
            }
            int value = head.val;
            head = head.next;
            head.prev = null;
            return value;
        }
        /**
         * 判空
         */
        public boolean empty() {
            return head == null;
        }
        /**
         * 获取头元素值
         */
        public int peek() {
            if(head == null) {
                return -1;
            }
            int value = head.val;
            return value;
        }
    }
}
